﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dt261.jsonmodel;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


//[Table("pd_audio_test_result")]
//public class SpeechResultItem
namespace Dt261.db
{
    class SpeechResultDb
    {

        private readonly SQLiteConnection connection;

        public SpeechResultDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            if (connection.CreateTable<SpeechResultItem>() > 0)
            {

            }
        }

        public int AddSpeechResult(SpeechResultItem m)
        {
            if (string.IsNullOrWhiteSpace(m.id))
            {
                throw new Exception(" id are required!");
                //return 0;
            }
            // 
            SpeechResultItem m2 = m.clone();
            return connection.Insert(m2);
        }

        public int UpdateSpeechResult(SpeechResultItem m)
        {
            if (string.IsNullOrWhiteSpace(m.id))
                throw new Exception(" name  are required!");

            IList<SpeechResultItem> list = connection.Query<SpeechResultItem>("select * from pd_audio_test_result where id = ?", m.id);

            if (list.Count() > 0)
            {
                return connection.Update(m);
            }
            else
            {
                return this.AddSpeechResult(m);
            }
        }

        public IList<SpeechResultItem> GetSpeechResults()
            => connection.Table<SpeechResultItem>().ToList();

        public IList<SpeechResultItem> GetSpeechResultByUid(string uid)
        {
            IList<SpeechResultItem> list = connection.Query<SpeechResultItem>("select * from pd_audio_test_result where id = ?", uid);

            return list;
        }

        public IList<SpeechResultItem> GetSpeechItemsByPersonId(string personId)
        {
            IList<SpeechResultItem> list = connection.Query<SpeechResultItem>("select * from pd_audio_test_result where personId = ?", personId);

            return list;
        }

        //public IList<MemberInfo> GetMembersByUserId(string userid)
        //{
        //    IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where UserId = ?  order by createTime DESC", userid);

        //    return list;
        //}

        //public MemberInfo GetOneMemberByUid(string uid)
        //{
        //    IList<MemberInfo> list = GetMemberByUid(uid);

        //    return list.Count() > 0 ? list[0] : null;
        //}

        //public MemberInfo GetOneMemberById(int id)
        //{
        //    IList<MemberInfo> list = GetMemberById(id);

        //    return list.Count() > 0 ? list[0] : null;
        //}

        public int DeleteSpeechResult(SpeechResultItem m)
        {
            if (m != null)
                return connection.Delete(m);
            return 0;
        }

        //public IList<MemberInfo> SearchMemberInfo(string userid, string key)
        //{
        //    if (key != null)
        //    {
        //        if (key == "ALL")
        //        {
        //            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where name like ?  order by createTime DESC", "%" + "%");
        //            return list;
        //        }
        //        else
        //        {
        //            IList<MemberInfo> list = connection.Query<MemberInfo>("select * from memberinfo where name like ? and  UserId = ?  order by createTime DESC", "%" + key + "%", userid);
        //            return list;
        //        }

        //    }

        //    return null;
        //}
    }
}



